<?php

namespace App\Console\Commands\OpenShop;

use App\Models\OpenShop\InvestmentPromotion\OpenShopLocationModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Models\OpenShop\StartBusiness\OpenShopStartBusinessModel;
use Illuminate\Console\Command;

class SyncOpenTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sync_open_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '同步开业时间到预计开业时间';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        OpenShopStartBusinessModel::query()
            ->join('open_shop_location', 'open_shop_location.orderNo', '=', 'open_shop_start_business.orderNo')
            ->where('open_shop_start_business.startBusinessTime', '>', 0)
            ->select(['open_shop_location.orderNo', 'open_shop_start_business.startBusinessTime', 'open_shop_location.openTime'])
            ->chunk(
                100, function ($item) {
                foreach ($item as $v) {
                    if ($v->openTime != $v->startBusinessTime) {
                        //同步预计开业时间
                        /*选址表单
                         * OpenShopSiteLocationModel::query()
                            ->where('orderNo', $v->orderNo)
                            ->update(
                                [
                                    'openTime' => $v->startBusinessTime,
                                ]
                            );*/
                        //开店 - 选址阶段主表
                        OpenShopLocationModel::query()
                            ->where('orderNo', $v->orderNo)
                            ->update(
                                [
                                    'openTime' => $v->startBusinessTime,
                                ]
                            );
                    }
                }
            }
            );

    }

}
